<template>
  <div>
    <el-card>
      商品类型搜索:
      <el-input
        placeholder="请输入"
        style="width: 200px; margin-right: 20px"
        v-model="params.className"
      ></el-input>
      <el-button type="primary" @click="searchFn">查询</el-button>
    </el-card>
    <el-card>
      <el-button style="background-color: orange; color: white" @click="addFn"
        >新建</el-button
      >
      <el-table :data="tableData">
        <el-table-column prop="indexId" label="序号"></el-table-column>
        <el-table-column
          prop="className"
          label="商品类型名称"
        ></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="edit(scope.row)">修改</el-button>
            <el-button type="text" @click="del(scope.row.classId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <form>
        商品类型名称:
        <el-input
          type="text"
          v-model="list.className"
          style="width: 250px"
        ></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmFn">确 定</el-button>
        </span>
      </form>
    </el-dialog>
  </div>
</template>

<script>
import { getSplxList, getSplxdel, getSplxedit, getSplxadd } from "@/api/spgl";
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      list: [],
      title: "修改商品类型",
      params: {
        pageIndex: 1,
        pageSize: 10,
        className: "",
      },
    };
  },
  methods: {
    async getSplxList() {
      const res = await getSplxList(this.params);
      console.log(res.data);
      this.tableData = res.data.currentPageRecords.map((item, index) => ({
        ...item,
        indexId: index + 1,
      }));
    },
    searchFn() {
      this.getSplxList();
    },
    edit(row) {
      console.log(row);
      this.title = "修改商品类型";
      this.dialogVisible = true;
      this.list = { ...row };
    },
    async confirmFn() {
      if (this.title === "修改商品类型") {
        //console.log(this.list.className);
        const res = await getSplxedit(
          { className: this.list.className },
          this.list.classId
        );
      } else {
        const res = await getSplxadd({ className: this.list.className });
        //console.log(res);
      }
      this.getSplxList();
      this.dialogVisible = false;
    },
    async del(id) {
      //console.log(id);
      const res = await getSplxdel(id);
      //console.log(res);
      this.getSplxList();
    },
    addFn() {
      this.title = "添加商品类型";
      this.dialogVisible = true;
      //console.log(this.list.className);
    },
  },
  created() {
    this.getSplxList();
  },
};
</script>

<style>
.el-card {
  margin-bottom: 20px;
}
</style>
